package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.InstitutionMoment;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.json.JSONObject;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.system.service.IInstitutionMomentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 机构动态信息
 *
 * @author ruoyi
 * @create 2021/5/9
 */
@Controller
@RequestMapping("/itsystem/institutionmoment")
public class InstitutionMomentController extends BaseController {

    /*页面前缀*/
    private String prefix = "itsystem/institutionmoment";

    @Autowired
    private IInstitutionMomentService institutionMomentService;

    @RequiresPermissions("itsystem:institutionmoment:view")
    @GetMapping()
    public String itmoment() {
        return prefix + "/institutionmoment";
    }

    /**
     * 获取当前机构所有动态信息
     *
     * @return 机构动态信息集合
     */
    @RequiresPermissions("itsystem:institutionmoment:list")
    @PostMapping("list")
    @ResponseBody
    public TableDataInfo selectAllMoment(InstitutionMoment institutionMoment) {
        //开启分页
        startPage();
        int itId = institutionMomentService.selectInstitutionIdByUserId(ShiroUtils.getUserId());

        //管理员机构id为0，不是管理员显示对应的机构动态信息
        if (itId != 0){
            institutionMoment.setItId(itId);
        }
        //获取机构动态信息集合
        List<InstitutionMoment> list = institutionMomentService.selectAllMoment(institutionMoment);
        return getDataTable(list);
    }

    /**
     * 根据机构动态id查询动态信息
     *
     * @param mId  机构动态信息
     * @param list 为存放机构动态信息实体
     * @return 机构动态信息
     */
    @RequiresPermissions("itsystem:institutionmoment:list")
    @PostMapping("selectOne")
    @ResponseBody
    public TableDataInfo selectMomentById(Long mId, List<InstitutionMoment> list) {
        //开启分页
        startPage();
        //获取机构动态信息集合
        InstitutionMoment institutionMoment = institutionMomentService.selectMomentById(mId);
        list.add(institutionMoment);
        return getDataTable(list);
    }

    /**
     * 新增机构动态信息
     *
     * @return 新增机构动态信息页面的路径
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 保存新添加的机构动态信息
     *
     * @param institutionMoment 机构动态信息
     * @return boolean (true 添加成功，false 添加失败)
     */
    @RequiresPermissions("itsystem:institutionmoment:add")
    @Log(title = "机构动态信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addMoment(InstitutionMoment institutionMoment) {
        int itId = institutionMomentService.selectInstitutionIdByUserId(ShiroUtils.getUserId());
        institutionMoment.setItId(itId);
        return toAjax(institutionMomentService.insertMoment(institutionMoment));
    }

    /**
     * 通过机构动态信息id获取要编辑的机构动态信息
     *
     * @param mId  机构动态id
     * @param mmap ModelMap映射(为存放机构动态信息实体)
     * @return 编辑机构动态信息页面的路径
     */
    @GetMapping("/edit/{mId}")
    public String editMoment(@PathVariable("mId") Long mId, ModelMap mmap) {
        InstitutionMoment InstitutionMoment = institutionMomentService.selectMomentById(mId);
        mmap.put("InstitutionMoment", InstitutionMoment);
        return prefix + "/edit";
    }

    /**
     * 保存修改后的机构动态信息
     *
     * @param institutionMoment 修改后的机构动态信息
     * @return boolean (true 修改成功，false 修改失败)
     */
    @RequiresPermissions("itsystem:institutionmoment:edit")
    @Log(title = "机构动态信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult updateMoment(InstitutionMoment institutionMoment) {
        return toAjax(institutionMomentService.updateMoment(institutionMoment));
    }

    /**
     * 删除机构动态信息
     *
     * @param mId 机构动态id
     * @return boolean (true 删除成功，false 删除失败)
     */
    /*@RequiresPermissions("itsystem:institutionmoment:remove")
    @Log(title = "机构动态信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult removeMoment(String mId)
    {
        System.out.println("==========================");
        System.out.println("mId = "+mId);
        boolean res = institutionMomentService.deleteMomentByIds(mId);
        return toAjax(res);
    }*/

    /**
     * 删除机构动态信息
     *
     * @param ids 需要删除的动态id
     * @return boolean (true 删除成功，false 删除失败)
     */
    @RequiresPermissions("itsystem:institutionmoment:remove")
    @Log(title = "机构动态信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult removeMomentByIds(String ids) {
        return toAjax(institutionMomentService.deleteMomentByIds(ids));
    }

}
